package fintecher.com.configuration;


import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * mybatis 配置数据源类
 *
 * @author zx
 */
@Configuration
@EnableTransactionManagement
public class MybatisConfiguration  {

        @Autowired
        private DruidProperties druidProperties;



    @Bean
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(druidProperties.getUrl());
        druidDataSource.setUsername(druidProperties.getUserName());
        druidDataSource.setPassword(druidProperties.getPassword());
        druidDataSource.setDriverClassName(StringUtils.isNotBlank(druidProperties.getDriveClassName())?druidProperties.getDriveClassName():"com.mysql.jdbc.Driver");
        druidDataSource.setMaxActive(StringUtils.isNotBlank(druidProperties.getMaxActive())? Integer.parseInt(druidProperties.getMaxActive()):10);
        druidDataSource.setInitialSize(StringUtils.isNotBlank(druidProperties.getInitialSize())? Integer.parseInt(druidProperties.getInitialSize()):1);
        druidDataSource.setMaxWait(StringUtils.isNotBlank(druidProperties.getMaxWait())? Integer.parseInt(druidProperties.getMaxWait()):60000);
        druidDataSource.setMinIdle(StringUtils.isNotBlank(druidProperties.getMinIdle())? Integer.parseInt(druidProperties.getMinIdle()):3);
        druidDataSource.setTimeBetweenEvictionRunsMillis(StringUtils.isNotBlank(druidProperties.getTimeBetweenEvictionRunsMillis())?
                Integer.parseInt(druidProperties.getTimeBetweenEvictionRunsMillis()):60000);
        druidDataSource.setMinEvictableIdleTimeMillis(StringUtils.isNotBlank(druidProperties.getMinEvictableIdleTimeMillis())?
                Integer.parseInt(druidProperties.getMinEvictableIdleTimeMillis()):300000);
        druidDataSource.setValidationQuery(StringUtils.isNotBlank(druidProperties.getValidationQuery())?druidProperties.getValidationQuery():"select 'x'");
        druidDataSource.setTestWhileIdle(StringUtils.isNotBlank(druidProperties.getTestWhileIdle())? Boolean.parseBoolean(druidProperties.getTestWhileIdle()):true);
        druidDataSource.setTestOnBorrow(StringUtils.isNotBlank(druidProperties.getTestOnBorrow())? Boolean.parseBoolean(druidProperties.getTestOnBorrow()):false);
        druidDataSource.setTestOnReturn(StringUtils.isNotBlank(druidProperties.getTestOnReturn())? Boolean.parseBoolean(druidProperties.getTestOnReturn()):false);
        druidDataSource.setPoolPreparedStatements(StringUtils.isNotBlank(druidProperties.getPoolPreparedStatements())? Boolean.parseBoolean(druidProperties.getPoolPreparedStatements()):true);
        druidDataSource.setMaxOpenPreparedStatements(StringUtils.isNotBlank(druidProperties.getMaxOpenPreparedStatements())? Integer.parseInt(druidProperties.getMaxOpenPreparedStatements()):20);

        try {
            druidDataSource.setFilters(StringUtils.isNotBlank(druidProperties.getFilters())?druidProperties.getFilters():"stat, wall");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return druidDataSource;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        bean.setTypeAliasesPackage(druidProperties.getTypeAliasesPackage());
//        bean.setVfs(SpringBootVFS.class);
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try {
            bean.setMapperLocations(resolver.getResources(druidProperties.getMapperLocations()));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }



    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
